function changeSucceedStyle(nameID, spanID) { //如果输入成功的话，用来修改后面span的内容样式和输入框的颜色

	spanID.firstChild.nodeValue = "*";
	spanID.style.fontSize = "larger";
	spanID.style.color = "green";
	nameID.style.borderColor = "limegreen";
}

function changeFailedStyle(nameID, spanID) { //输入失败而且焦点不在该输入框时的样式

	spanID.firstChild.nodeValue = "*"; //设置后面的字体颜色、格式、大小

	spanID.style.fontSize = "larger";
	spanID.style.color = "red";
	nameID.style.borderColor = "red";
}

function changeFailingStyle(nameID, spanID) { //输入失败时但是焦点还在该输入框时的样式

	spanID.style.fontSize = "small";
	spanID.style.color = "red";
	nameID.style.borderColor = "red";
}


function vercode() {
	var vercode = $("#vercode").get(0);
	var vercodeSpan = $("#vercodeSpan").get(0);
	vercode.onfocus = function() {
		if(vercode.value == "" || vercodeSpan.style.color == "red" || vercodeSpan.style.color == "") { //如果内容为空点击产生验证码

			produceCode(); //在firefox浏览器中 刚开始vercodeSpan的颜色是空的

		} else { //其他情况下不产生验证码，只是对验证码对错进行验证

			if(vercodeSpan.style.color == "red") { //如果验证码不正确，根据提示语句的颜色来确认是否正确

				vercodeSpan.style.color = "red"; //单独设置样式

				vercode.style.borderColor = "red";
			} else { //在chrome不需要  在ie中需要  输入完验证码 再输入密码自动跳转时

				changeSucceedStyle(vercode, vercodeSpan);
			}
		}
	}
	vercode.onkeyup = function(s) {
		var vercodeSpan = $("#vercodeSpan").get(0);
		if(vercode.value == vercodeSpan.firstChild.nodeValue) { //如果验证码正确

			changeSucceedStyle(vercode, vercodeSpan);
		} else {
			vercodeSpan.style.color = "red"; //单独设置样式

			vercode.style.borderColor = "red";
		}
		if(s == 1) { //如果修改前是正确的

			produceCode();
			vercodeSpan.style.color = "red"; //单独设置样式

			vercode.style.borderColor = "red";
		}
	}
	vercode.onkeydown = function() { //修改前正确的话重新产生验证码

		if(window.event.keyCode) {
			var k = window.event.keyCode; //得到键盘此时按下的值   !!!firefox浏览器不支持这种事件

			if(vercodeSpan.style.color == "green" && k != 9) { //如果此时按下的是tab键的话 不调用此函数

				vercode.onkeyup(1);
			}
		} else {
			if(vercodeSpan.style.color == "green") { //如果此时按下的是tab键的话 不调用此函数

				vercode.onkeyup(1);
			}
		}

	}
	vercode.onblur = function() {
		if(vercodeSpan.style.color == "green") {
			changeSucceedStyle(vercode, vercodeSpan);
			vercodenum = 1;
		} else {
			changeFailedStyle(vercode, vercodeSpan);
			vercodenum = 0
		}
	}

	function produceCode() { //产生验证码

		var code = ""; //初始化一个字符串型的空验证码

		for(var i = 0; i < 4; i++) { //循环四次

			var num = Math.floor(Math.random() * 10);
			code += num; //产生一个数字加到验证码上

		}
		vercodeSpan.firstChild.nodeValue = code; //设置span为验证码

	}
}

function username() {

	var username = $("#username").get(0); //得到账户的对象

	var usernameSpan = $("#usernameSpan").get(0); //得到文本对象
    
	/*for (var i = 0; i < localStorage.length; i++) {
                    var sitename = localStorage.key(i);
                    var str = localStorage.getItem(sitename);
                  if (typeof str === 'string') {
                        var site = JSON.parse(str);
                    }
				  if(username.value == site.name ) break;
					}*/

	username.onfocus = function() { //获得焦点时根据匹配成功与否修改span中的样式和内容
        	for (var i = 0; i < localStorage.length; i++) {
                    var sitename = localStorage.key(i);
                    var str = localStorage.getItem(sitename);
                  if (typeof str === 'string') {
                        var site = JSON.parse(str);
                    }
				  if(username.value == site.name ) break;
					}

		if(username.value != site.name ) { //如果获得焦点时输入不正确，重新调整样式

			usernameSpan.firstChild.nodeValue = "账户不存在"; //修改提示语句

			changeFailingStyle(username, usernameSpan); //修改为匹配中并且失败的样式

		} else {
			changeSucceedStyle(username, usernameSpan);
			usernamenum = 1;
		}
	}
	username.onkeyup = function() { //输入内容是判断根据输入的值修改span中的样式和内容,使用up不是down，因为down读取时候有出入

		
	    for (var i = 0; i < localStorage.length; i++) {
                    var sitename = localStorage.key(i);
                    var str = localStorage.getItem(sitename);
                  if (typeof str === 'string') {
                        var site = JSON.parse(str);
                    }
				  if(username.value == site.name ) break;
					}
		if(username.value == site.name) { //匹配成功的话

			changeSucceedStyle(username, usernameSpan); //修改为匹配成功的样式

			password.focus(); //输入正确的话直接换行

			if(password.value != "") { //如果此时密码的值不为空

				password.onfocus();
			}
		} else { //匹配失败

			usernameSpan.firstChild.nodeValue = "账户不存在"; //修改提示语句

			changeFailingStyle(username, usernameSpan);
			if(password.value != "") { //如果此时密码的值不为空

				//password.onfocus();

			}
		}
	}
	username.onblur = function() { //失去焦点时根据匹配成功与否修改span中的样式和内容

		//usernameCookie = getCookie(username.value + "username");
        for (var i = 0; i < localStorage.length; i++) {
                    var sitename = localStorage.key(i);
                    var str = localStorage.getItem(sitename);
                  if (typeof str === 'string') {
                        var site = JSON.parse(str);
                    }
				  if(username.value == site.name ) break;
					}
		if(username.value == site.name) { //匹配成功的话

			changeSucceedStyle(username, usernameSpan); //修改为成功的样式

			usernamenum = 1;
		} else { //匹配失败

			changeFailedStyle(username, usernameSpan); //修改为失败的样式

			usernamenum = 0;
		}
	}
}

function password() {
    
	var password = $("#password").get(0); //得到账户的对象

	var passwordSpan = $("#passwordSpan").get(0); //得到文本对象

	var username = $("#username").get(0); //得到用户所输入的账户

	var vercode = $("#vercode").get(0);
	
	/*for (var i = 0; i < localStorage.length; i++) {
                    var sitename = localStorage.key(i);
                    var str = localStorage.getItem(sitename);
                  if (typeof str === 'string') {
                        var site = JSON.parse(str);
                    }
				  if(username.value == site.name ) break;
					}*/
	
	password.onfocus = function() { //获得焦点时根据匹配成功与否修改span中的样式和内容

		//再次得到密码和用户名，防止填写完密码后修改用户名导致用户名和密码不一致
        for (var i = 0; i < localStorage.length; i++) {
                    var sitename = localStorage.key(i);
                    var str = localStorage.getItem(sitename);
                  if (typeof str === 'string') {
                        var site = JSON.parse(str);
                    }
				  if(username.value == site.name ) break;
					}
		if(!(password.value == site.password) || !(username.value == site.name)) { //如果获得焦点时输入不正确，重新调整样式

			passwordSpan.firstChild.nodeValue = "密码错误"; //修改提示语句

			changeFailingStyle(password, passwordSpan); //修改为匹配中并且失败的样式

		} else {
			changeSucceedStyle(password, passwordSpan);
		}
	}
	password.onkeyup = function() { //输入内容是判断根据输入的值修改span中的样式和内容,使用up不是down，因为down读取时候有出入
  
		for (var i = 0; i < localStorage.length; i++) {
                    var sitename = localStorage.key(i);
                    var str = localStorage.getItem(sitename);
                  if (typeof str === 'string') {
                        var site = JSON.parse(str);
                    }
				  if(username.value == site.name ) break;
					}
		if(password.value == site.password && username.value == site.name) { //如果密码等于cookie的值，并且用户输入的账户与cookie的值一致时

			changeSucceedStyle(password, passwordSpan); //修改为匹配成功的样式

			vercode.focus(); //输入正确的话直接换行

		} else { //匹配失败

			passwordSpan.firstChild.nodeValue = "密码错误"; //修改提示语句

			changeFailingStyle(password, passwordSpan);
		}
	}
	password.onblur = function() { //失去焦点时根据匹配成功与否修改span中的样式和内容

		//usernameCookie = getCookie(username.value + "username");

		//passwordCookie = getCookie(username.value + "password");
        for (var i = 0; i < localStorage.length; i++) {
                    var sitename = localStorage.key(i);
                    var str = localStorage.getItem(sitename);
                  if (typeof str === 'string') {
                        var site = JSON.parse(str);
                    }
				  if(username.value == site.name ) break;
					}
		if(password.value == site.password && username.value == site.name) { //匹配成功的话

			changeSucceedStyle(password, passwordSpan); //修改为成功的样式

			passwordnum = 1;
		} else { //匹配失败

			changeFailedStyle(password, passwordSpan); //修改为失败的样式

			passwordnum = 0;
		}
	}
}

var usernamenum = 0,
	passwordnum = 0,
	vercodenum = 0;

function user() {
	var user = $("#user").get(0);
	var username = $("#username").get(0);
	user.onclick = function() {
		if(usernamenum && passwordnum && vercodenum) {
            window.location.href = 'web4.html';

		} else {
			alert("请确认填写的信息全部正确！");
			return false;
		}
	}
}

$( function() {
	vercode();
	username();
	password();
	user();
	var Username = $("#username").get(0);
	Username.onfocus();
	//var a = unescape(document.cookie);


			//alert(a);


});